package cn.kgc.thread.demo3;

import java.util.concurrent.*;

/**
 * @description:
 * @author: 石浩杰
 * @date: 2022-12-14 18:41
 */
public class Demo2 {
    private static ThreadPoolExecutor executor=new ThreadPoolExecutor(10,10,1, TimeUnit.SECONDS,new LinkedBlockingDeque<>(10), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());

    public static void main(String[] args) {
        CompletableFuture<String> fature1=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务1中的内容");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1执行完毕");
            return "任务1的返回值";
        },executor);
        CompletableFuture<String> fature2=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务2中的内容");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2执行完毕");
            return "任务2的返回值";
        },executor);
        //CompletableFuture.allOf(fature1,fature2).join();
        //System.out.println("等待任务1和任务2执行完毕后执行");
        CompletableFuture.anyOf(fature1,fature2).join();
        System.out.println("只要有一个任务执行完毕后执行");
    }

    private static void extracted7() throws InterruptedException,ExecutionException{
        CompletableFuture<String> future1=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务1中的内容");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1执行完毕");
            return "任务1的返回值";
        },executor);
        CompletableFuture<String> future2=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务2中的内容");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2执行完毕");
            return "任务2的返回值";
        },executor);
        CompletableFuture<String> future3=future1.applyToEitherAsync(future2, result1 -> {
            System.out.println("执行任务3中的内容");
            System.out.println("任务3中的前两个任务中的一个任务返回值是:"+result1);
            return "任务3的返回值";
        },executor);
        System.out.println("任务3的返回值是:"+future3.get());

        CompletableFuture<Void> future4=future1.thenAcceptBothAsync(future2, (result1,result2)->{
            System.out.println("执行任务4中的内容");
            System.out.println("任务4中的前两个任务的返回值是:"+result1+"和"+result2);
        },executor);
        System.out.println("任务4的返回值是："+future4.get());

        CompletableFuture<Void> future5=future1.runAfterBothAsync(future2,()->{
            System.out.println("执行任务5中的内容，需要等待任务1和任务2执行完毕之后执行");
        },executor);
        System.out.println("任务5的返回值是："+future5.get());
    }

    private static void extracted5(){
        CompletableFuture<Void> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务1中的内容");
            return "任务1的返回值";
        },executor).thenRunAsync(()->{
            System.out.println("执行任务2中的内容");
        },executor);
    }

    private static void extracted4(){
        CompletableFuture<Void> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务1中的内容");
            return "任务1的返回值";
        },executor).thenAcceptAsync(result->{
            System.out.println("执行任务2中的内容");
            System.out.println("任务2中的任务1的返回值是:"+result);
        },executor);
    }

    private static void extracted3()throws InterruptedException, ExecutionException{
        CompletableFuture<String> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("执行任务1中的内容");
            return "任务1的返回值";
        },executor).thenApplyAsync(result->{
            System.out.println("执行任务2中的内容");
            System.out.println("任务2中的任务1的返回值是:"+result);
            return "任务2的返回值";
        },executor);
    }
    private static void extracted2() throws InterruptedException, ExecutionException {
        CompletableFuture<String> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("任务1开始执行");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1执行完毕");
            return "任务1的返回值";
        },executor).handleAsync((result,exception)->{
            System.out.println("任务2开始执行,任务2中接收到任务1的返回值:"+result);
            return "任务2的返回值";
        },executor);
        System.out.println(completableFuture.get());
    }
    private static void extracted1()throws InterruptedException, ExecutionException{
        CompletableFuture<String> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("任务1开始执行");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1执行完毕");
            return "任务1的返回值";
        },executor).whenCompleteAsync((result,exception)->{
            System.out.println("任务2开始执行,任务2中接收到任务1的返回值:"+result);
        },executor).exceptionally(exception->{
            System.out.println("任务2执行出现异常");
            return "任务2的返回值";
        });
    }
}
